package com.wzdigit.wms.wms.service.basic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateZoneRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchZoneRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusZoneRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateZoneRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ZoneApiResponse;
import com.wzdigit.wms.basic.client.response.basic.ZoneResponse;
import com.wzdigit.wms.basic.domain.basic.Zone;
import com.wzdigit.wms.basic.enums.BaseIsEnabledEnum;
import com.wzdigit.wms.basic.mapper.core.basic.ZoneMapper;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.common.entity.StatusCode;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 库区 服务类
 * @author yangdongjie
 * @ZoneAttrService.java
 * @2021年1月4日上午11:16:31
 */
@Service
public class ZoneService {

    @Autowired
    private ZoneMapper zoneMapper;

	public List<ZoneApiResponse> list(Integer whsId) {
		return zoneMapper.selectAllByWhsIdAndStatus(whsId, BaseIsEnabledEnum.USE.code);
	}

	//根据区域id集合查询库区下拉列表
	public List<ZoneApiResponse> listAllZoneGroupsIds(Integer zoneGroupsId) {
		return zoneMapper.selectAllByZoneGroupIds(zoneGroupsId);
	}
    
    public ZoneResponse getZone(Integer zoneId) {
    	return zoneMapper.selectOneByZoneId(zoneId);
    }
    
    @SuppressWarnings("rawtypes")
	public <T> PagerInfo<List<ZoneResponse>> searchZone(SearchZoneRequest dto) {
    	Page<T> page = PageHelper.offsetPage(dto.getSkipCount(), dto.getMaxResultCount());
        PagerInfo<List<ZoneResponse>> pagerInfo = new PagerInfo<List<ZoneResponse>>();
        List<ZoneResponse> list = zoneMapper.selectAll(dto);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneResponse createZone(CreateZoneRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Zone zone = zoneMapper.selectOneByZoneCode(dto.getZoneCode());
    	if(zone != null) {
    		// 编码已存在
    		Shift.fatal(StatusCode.CODE_DATA_EXIST);
    	}
    	zone = new Zone();
    	BeanUtil.copyNonNull(dto, zone);
    	zone.setCreateUser(loginName);
    	zone.setCreateTime(new Date());
    	zoneMapper.insert(zone);
    	
    	ZoneResponse response = new ZoneResponse();
    	BeanUtil.copyNonNull(zone, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneResponse updateZone(UpdateZoneRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Zone zone = zoneMapper.selectOneByZoneCode(dto.getZoneCode());
    	if(zone != null) {
    		if (dto.getZoneId().intValue() != zone.getZoneId().intValue()) {
    			// 编码已存在
    			Shift.fatal(StatusCode.CODE_DATA_EXIST);
			}
    	}
    	zone = zoneMapper.selectByPrimaryKey(dto.getZoneId());
    	BeanUtil.copy(dto, zone);
    	zone.setModifyUser(loginName);
    	zone.setModifyTime(new Date());
    	zoneMapper.updateById(zone);
    	
    	ZoneResponse response = new ZoneResponse();
    	BeanUtil.copy(zone, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> updateStatus(UpdateStatusZoneRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	Zone zone = zoneMapper.selectByPrimaryKey(dto.getZoneId());
    	zone.setStatus(dto.getStatus());
    	zone.setModifyUser(loginName);
    	zone.setModifyTime(new Date());
    	zoneMapper.updateStatus(zone);
		return new Result<>();
    }

	/**
	 * 根据主键查询库区
	 * @param zoneId
	 * @return
	 */
	public Zone getById(Integer zoneId){
    	return zoneMapper.selectById(zoneId);
	}

	/**
	 * 根据库区ID集合查询库区集合
	 * @param zoneIdList
	 * @return
	 */
	public List<Zone> getZoneListByZoneIdList(List<Integer> zoneIdList) {

		//创建类型的条件构造器
		QueryWrapper<Zone> wrapper = new QueryWrapper<>();
		wrapper.in("ZONE_ID", zoneIdList);

		List<Zone> zoneList = zoneMapper.selectList(wrapper);

		return zoneList;
	}
}
